'use strict';

const LZString = {
  _keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
  e: 'base64',
  _a() {
    let a,
      b,
      c,
      d,
      e,
      f,
      g = arguments[0] || {},
      h = 1,
      i = arguments.length,
      j = !1,
      n = function(a, b) {
        return new n.fn.init(a, b);
      };
    console.log(arguments[0]);
    for (typeof g === 'boolean' && (j = g,
    g = arguments[h] || {},
    h++),
    typeof g === 'object' || n.isFunction(g) || (g = {}),
    h === i && (g = this,
    h--); i > h; h++) {
      if ((e = arguments[h]) != null) {
        for (d in e) {
          a = g[d],
          c = e[d],
          g !== c && (j && c && (n.isPlainObject(c) || (b = n.isArray(c))) ? (b ? (b = !1,
          f = a && n.isArray(a) ? a : []) : f = a && n.isPlainObject(a) ? a : {},
          g[d] = n.extend(j, f, c)) : void 0 !== c && (g[d] = c));
        }
      }
    }
    console.log('g: ', g);
    return g;
  },
  compress(e) {
    if (e == null) {
      return '';
    }
    let h,
      l,
      n = {},
      m = {},
      o = '',
      c = '',
      r = '',
      d = 2,
      g = 3,
      b = 2,
      q = '',
      a = 0,
      j = 0,
      p,
      k = String.fromCharCode;
    for (p = 0; p < e.length; p += 1) {
      o = e.charAt(p);
      if (!Object.prototype.hasOwnProperty.call(n, o)) {
        n[o] = g++;
        m[o] = true;
      }
      c = r + o;
      if (Object.prototype.hasOwnProperty.call(n, c)) {
        r = c;
      } else {
        if (Object.prototype.hasOwnProperty.call(m, r)) {
          if (r.charCodeAt(0) < 256) {
            for (h = 0; h < b; h++) {
              a = (a << 1);
              if (j === 15) {
                j = 0;
                q += k(a);
                a = 0;
              } else {
                j++;
              }
            }
            l = r.charCodeAt(0);
            for (h = 0; h < 8; h++) {
              a = (a << 1) | (l & 1);
              if (j === 15) {
                j = 0;
                q += k(a);
                a = 0;
              } else {
                j++;
              }
              l = l >> 1;
            }
          } else {
            l = 1;
            for (h = 0; h < b; h++) {
              a = (a << 1) | l;
              if (j === 15) {
                j = 0;
                q += k(a);
                a = 0;
              } else {
                j++;
              }
              l = 0;
            }
            l = r.charCodeAt(0);
            for (h = 0; h < 16; h++) {
              a = (a << 1) | (l & 1);
              if (j === 15) {
                j = 0;
                q += k(a);
                a = 0;
              } else {
                j++;
              }
              l = l >> 1;
            }
          }
          d--;
          if (d === 0) {
            d = Math.pow(2, b);
            b++;
          }
          delete m[r];
        } else {
          l = n[r];
          for (h = 0; h < b; h++) {
            a = (a << 1) | (l & 1);
            if (j === 15) {
              j = 0;
              q += k(a);
              a = 0;
            } else {
              j++;
            }
            l = l >> 1;
          }
        }
        d--;
        if (d === 0) {
          d = Math.pow(2, b);
          b++;
        }
        n[c] = g++;
        r = String(o);
      }
    }
    if (r !== '') {
      if (Object.prototype.hasOwnProperty.call(m, r)) {
        if (r.charCodeAt(0) < 256) {
          for (h = 0; h < b; h++) {
            a = (a << 1);
            if (j === 15) {
              j = 0;
              q += k(a);
              a = 0;
            } else {
              j++;
            }
          }
          l = r.charCodeAt(0);
          for (h = 0; h < 8; h++) {
            a = (a << 1) | (l & 1);
            if (j === 15) {
              j = 0;
              q += k(a);
              a = 0;
            } else {
              j++;
            }
            l = l >> 1;
          }
        } else {
          l = 1;
          for (h = 0; h < b; h++) {
            a = (a << 1) | l;
            if (j === 15) {
              j = 0;
              q += k(a);
              a = 0;
            } else {
              j++;
            }
            l = 0;
          }
          l = r.charCodeAt(0);
          for (h = 0; h < 16; h++) {
            a = (a << 1) | (l & 1);
            if (j === 15) {
              j = 0;
              q += k(a);
              a = 0;
            } else {
              j++;
            }
            l = l >> 1;
          }
        }
        d--;
        if (d === 0) {
          d = Math.pow(2, b);
          b++;
        }
        delete m[r];
      } else {
        l = n[r];
        for (h = 0; h < b; h++) {
          a = (a << 1) | (l & 1);
          if (j === 15) {
            j = 0;
            q += k(a);
            a = 0;
          } else {
            j++;
          }
          l = l >> 1;
        }
      }
      d--;
      if (d === 0) {
        d = Math.pow(2, b);
        b++;
      }
    }
    l = 2;
    for (h = 0; h < b; h++) {
      a = (a << 1) | (l & 1);
      if (j === 15) {
        j = 0;
        q += k(a);
        a = 0;
      } else {
        j++;
      }
      l = l >> 1;
    }
    while (true) {
      a = (a << 1);
      if (j === 15) {
        q += k(a);
        break;
      } else {
        j++;
      }
    }
    return q;
  },
  compressToBase64(c) {
    if (c == null) {
      return '';
    }
    let a = '';
    let k,
      h,
      f,
      j,
      g,
      e,
      d;
    let b = 0;
    c = LZString.compress(c);
    // console.log(c)
    while (b < c.length * 2) {
      if (b % 2 === 0) {
        k = c.charCodeAt(b / 2) >> 8;
        h = c.charCodeAt(b / 2) & 255;
        if (b / 2 + 1 < c.length) {
          f = c.charCodeAt(b / 2 + 1) >> 8;
        } else {
          f = NaN;
        }
      } else {
        k = c.charCodeAt((b - 1) / 2) & 255;
        if ((b + 1) / 2 < c.length) {
          h = c.charCodeAt((b + 1) / 2) >> 8;
          f = c.charCodeAt((b + 1) / 2) & 255;
        } else {
          h = f = NaN;
        }
      }
      b += 3;
      j = k >> 2;
      g = ((k & 3) << 4) | (h >> 4);
      e = ((h & 15) << 2) | (f >> 6);
      d = f & 63;
      if (isNaN(h)) {
        e = d = 64;
      } else {
        if (isNaN(f)) {
          d = 64;
        }
      }
      a = a + LZString._keyStr.charAt(j) + LZString._keyStr.charAt(g) + LZString._keyStr.charAt(e) + LZString._keyStr.charAt(d);
    }
    // console.log(a)
    return a;
  },
  _compressJSON(c, d) {
    if (!d || (d === 'base64')) {
      return LZString.compressToBase64(JSON.stringify(c));
    }
    // if (d == "utf16") {
    //     return compressToUTF16(JSON.stringify(c))
    // }
    return false;

  },
  getCurrentStateString(cu) {
    // var c = LZString._a(cu, d || {}, {
    //     _: LZString.scriptVersion
    // });
    return LZString._compressJSON(cu, LZString.e);
  },
};

if (typeof module !== 'undefined' && module != null) {
  module.exports = LZString;
}
